package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabFeeItem;
import com.ysd.lis.entity.lab.LabFinanceSalesFeeitem;
import com.ysd.lis.entity.lab.LabInspectCompanyFeeitem;
import com.ysd.lis.entity.lab.LabInspectCompanySalesman;
import com.ysd.lis.mapper.lab.LabFinanceSalesFeeitemMapper;
import com.ysd.lis.mapper.lab.LabInspectCompanyFeeitemMapper;
import com.ysd.lis.request.lab.SalesFeeItemDto;
import com.ysd.lis.service.lab.LabFinanceSalesFeeitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 销售项目中间表 服务实现类
 * </p>
 *
 * @author WH
 * @since 2024-11-07
 */
@Service
public class LabFinanceSalesFeeitemServiceImpl extends ServiceImpl<LabFinanceSalesFeeitemMapper, LabFinanceSalesFeeitem> implements LabFinanceSalesFeeitemService {

    @Autowired
    LabFinanceSalesFeeitemMapper labFinanceSalesFeeitemMapper;
    @Autowired
    LabInspectCompanyFeeitemMapper labInspectCompanyFeeitemMapper;
    @Override
    public Result findSalesFeeItemList(SalesFeeItemDto param) {
        MPJLambdaWrapper<LabInspectCompanyFeeitem> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAs(LabInspectCompanyFeeitem::getCompanyId, LabFinanceSalesFeeitem::getCompanyId);
        queryWrapper.selectAs(LabInspectCompanyFeeitem::getCompanyCode, LabFinanceSalesFeeitem::getCompanyCode);
        queryWrapper.selectAs(LabInspectCompanyFeeitem::getFeeId, LabFinanceSalesFeeitem::getFeeId);
        queryWrapper.selectAs(LabInspectCompanyFeeitem::getFeeCode, LabFinanceSalesFeeitem::getFeeCode);
        queryWrapper.selectAs(LabInspectCompanyFeeitem::getOrigPrice, LabFinanceSalesFeeitem::getOrigPrice);
        queryWrapper.selectAs(LabInspectCompanyFeeitem::getDiscount, LabFinanceSalesFeeitem::getDiscount);
        queryWrapper.selectAs(LabInspectCompanyFeeitem::getPrice, LabFinanceSalesFeeitem::getPrice);
        queryWrapper.selectAs("cs", LabInspectCompanySalesman::getCode, LabFinanceSalesFeeitem::getSalesmanCode);
        queryWrapper.selectAs("cs", LabInspectCompanySalesman::getName, LabFinanceSalesFeeitem::getSalesmanName);
        queryWrapper.selectAs("fi", LabFeeItem::getFeeName, LabFinanceSalesFeeitem::getFeeName);
        queryWrapper.selectAs("sf", LabFinanceSalesFeeitem::getId, LabFinanceSalesFeeitem::getId);
        queryWrapper.selectAs("sf", LabFinanceSalesFeeitem::getBrokerageType, LabFinanceSalesFeeitem::getBrokerageType);
        queryWrapper.selectAs("sf", LabFinanceSalesFeeitem::getBrokerage, LabFinanceSalesFeeitem::getBrokerage);
        queryWrapper.selectAs("sf", LabFinanceSalesFeeitem::getBrokerageCosts, LabFinanceSalesFeeitem::getBrokerageCosts);

        queryWrapper.leftJoin(LabInspectCompanySalesman.class,"cs",p->p.eq(LabInspectCompanySalesman::getCompanyCode,LabInspectCompanyFeeitem::getCompanyCode).eq(LabInspectCompanySalesman::getCode,param.getSalesmanCode()).eq(LabInspectCompanySalesman::getDelFlag,0));
        queryWrapper.leftJoin(LabFeeItem.class,"fi", p->p.eq(LabFeeItem::getFeeCode,LabInspectCompanyFeeitem::getFeeCode).eq(LabFeeItem::getDelFlag,0));
        queryWrapper.leftJoin(LabFinanceSalesFeeitem.class,"sf", p->p.eq(LabFinanceSalesFeeitem::getCompanyCode,LabInspectCompanyFeeitem::getCompanyCode).eq(LabFinanceSalesFeeitem::getFeeCode,LabInspectCompanyFeeitem::getFeeCode).eq(LabFinanceSalesFeeitem::getSalesmanCode,param.getSalesmanCode()).eq(LabFinanceSalesFeeitem::getDelFlag,0));
        queryWrapper.eq(LabInspectCompanyFeeitem::getCompanyCode,param.getCompanyCode())
                .eq(LabInspectCompanyFeeitem::getDelFlag,0);

        List<LabFinanceSalesFeeitem> salesFeeitemList = labInspectCompanyFeeitemMapper.selectJoinList(LabFinanceSalesFeeitem.class, queryWrapper);

        return Result.succ(1, "查询成功", salesFeeitemList);

    }

    @Override
    public Result saveBatchSalesFeeitem(SalesFeeItemDto dto) {

        LambdaUpdateWrapper<LabFinanceSalesFeeitem> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(LabFinanceSalesFeeitem::getSalesmanCode, dto.getSalesmanCode());
        lambdaUpdateWrapper.eq(LabFinanceSalesFeeitem::getCompanyCode, dto.getCompanyCode());

        labFinanceSalesFeeitemMapper.delete(lambdaUpdateWrapper);

        List<LabFinanceSalesFeeitem> financeSalesFeeitemList = dto.getFinanceSalesFeeitemList();
        boolean b = this.saveBatch(financeSalesFeeitemList);
        if(b){
            return Result.succ(1, "保存成功",null);

        }else {
            return Result.fail("保存失败");
        }
    }
}
